package com.jack.thinking.java._22.swing.swt;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;

import com.utils.SWTConsole;

public class TabbedPane implements SWTApplication{

	private static TabFolder folder;
	private static Shell shell;
	
	@Override
	public void createContents(Composite parent) {
		shell = parent.getShell();
		parent.setLayout(new FillLayout());
		folder = new TabFolder(shell, SWT.BORDER);
		labelTab();
		directoryDialogTab();
		buttonTab();
		sliderTab();
		scribbleTab();
		browserTab();
	}
	private static void browserTab() {
		TabItem tab = new TabItem(folder, SWT.CLOSE);
		tab.setText("A Browser");
		tab.setToolTipText("A Web brower");
		Browser brower = null;
		try {
			brower  = new Browser(folder, SWT.NONE);
		} catch (Exception e) {
			Label label = new Label(folder, SWT.BORDER);
			label.setText("Could not initialize brower");
			tab.setControl(label);
		}
		if(brower != null) {
			brower.setUrl("http://www.baidu.com");
			tab.setControl(brower);
		}
	}
	private static void scribbleTab() {
		TabItem tab = new TabItem(folder, SWT.CLOSE);
		tab.setText("Scribble");
		tab.setToolTipText("Simple graphics: drawing");
		final Canvas canvas = new Canvas(folder, SWT.NONE);
		ScribbleMouseListener sml = new ScribbleMouseListener();
		canvas.addMouseListener(sml);
		canvas.addMouseMoveListener(sml);
		tab.setControl(canvas);
	}
	private static void sliderTab() {
		TabItem tab = new TabItem(folder, SWT.CLOSE);
		tab.setText("Sliders and Progress bars");
		tab.setToolTipText("Tied Slider to ProgressBar");
		Composite composite = new Composite(folder, SWT.NONE);
		composite.setLayout(new GridLayout(2, true));
		final Slider slider = new Slider(composite, SWT.HORIZONTAL);
		final ProgressBar progress = new ProgressBar(composite, SWT.HORIZONTAL);
		slider.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				progress.setSelection(slider.getSelection());
			}
		});
		tab.setControl(composite);
	}
	private static void buttonTab() {
		TabItem tab = new TabItem(folder, SWT.CLOSE);
		tab.setText("Buttons");
		tab.setToolTipText("Different kinds of Buttons");
		Composite composite = new Composite(folder, SWT.NONE);
		composite.setLayout(new GridLayout(4, true));
		for(int dir : new int[] {SWT.UP, SWT.RIGHT, SWT.LEFT, SWT.DOWN}) {
			Button b = new Button(composite, SWT.ARROW |dir);
			b.addListener(SWT.MouseDown, listener);
		}
		newButton(composite, SWT.CHECK, "Check button");
		newButton(composite, SWT.PUSH, "PUSH button");
		newButton(composite, SWT.RADIO, "RADIO button");
		newButton(composite, SWT.TOGGLE, "TOGGLE button");
		newButton(composite, SWT.FLAT, "FLAT button");
		tab.setControl(composite);
	}
	private static void directoryDialogTab() {
		TabItem tab = new TabItem(folder, SWT.CLOSE);
		tab.setText("Directory Dialog");
		tab.setToolTipText("Select a directory");
		final Button b = new Button(folder, SWT.PUSH);
		b.setText("Select a Directory");
		b.addListener(SWT.MouseDown, new Listener() {

			@Override
			public void handleEvent(Event arg0) {
				DirectoryDialog dd = new DirectoryDialog(shell);
				String path = dd.open();
				if(path != null) {
					b.setText(path);
				}
			}
			
		});
		tab.setControl(b);
	}
	private static void labelTab() {
		TabItem tab = new TabItem(folder, SWT.CLOSE);
		tab.setText("A Label");
		tab.setToolTipText("A simple label");
		Label label = new Label(folder, SWT.CENTER);
		label.setText("Label text");
		tab.setControl(label);
	}
	private static Listener listener = new Listener(){

		@Override
		public void handleEvent(Event e) {
			MessageBox m = new MessageBox(shell, SWT.OK);
			m.setMessage(e.toString());
			m.open();
		}	
		
	};
	private static void newButton(Composite composite, int type, String label) {
		Button b = new Button(composite, type);
		b.setText(label);
		b.addListener(SWT.MouseDown, listener);
	}
	
	private static class ScribbleMouseListener extends MouseAdapter implements MouseMoveListener{
		private Point p = new Point(0, 0);
		@Override
		public void mouseMove(MouseEvent e) {
			if((e.stateMask & SWT.BUTTON1)  == 0) {
				return;
			}
			GC gc = new GC((Canvas) e.widget);
			System.out.println(e.x);
			gc.drawLine(p.x, p.y, e.x, e.y);
			gc.dispose();
			updatePoint(e);
		}
		
		@Override
		public void mouseDown(MouseEvent e) {
			updatePoint(e);
		}
		
		private void updatePoint(MouseEvent e) {
			p.x = e.x;
			p.y = e.y;
		}
		
	}
	
	public static void main(String[] args) {
		SWTConsole.run(new TabbedPane(), 800, 600);
	}

}
